package com.ee.chat.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.AdminMenuMapper;
import com.ee.chat.common.dao.AdminOperationLogMapper;
import com.ee.chat.common.entity.AdminMenu;
import com.ee.chat.common.entity.AdminOperationLog;
import com.ee.chat.common.entity.AdminUser;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.IPUtil;
import com.ndhutu.ee.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author Shane
 * @Date 2019/11/30 19:24
 */
@Service
public class OperationServiceImpl implements IOperationService {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    AdminOperationLogMapper adminOperationLogMapper;
    @Resource
    AdminMenuMapper adminMenuMapper;

    @Override
    public void addOperationLog(HttpServletRequest request, String describe) throws Exception {
        HttpSession session = request.getSession(true);

        AdminUser adminUser = (AdminUser) session.getAttribute("adminUser");
        String ip = IPUtil.getClientIP(request);

        String uri = request.getRequestURI();
        uri = uri.substring(uri.indexOf("/", 0));
        AdminMenu adminMenu = adminMenuMapper.selectByUrl(uri);

        AdminOperationLog adminOperationLog = new AdminOperationLog(adminUser.getId(), ip, adminMenu == null ? null : adminMenu.getId(), describe);
        adminOperationLogMapper.insert(adminOperationLog);

        log.info(adminUser.getUsername() + "用户" + describe);
    }

    @Override
    public Result list(HttpServletRequest request, Integer page, Integer limit, String keyword, String describe, String createTime) throws Exception {
        Map<String, Object> data = new HashMap<>();

        if(StringUtil.isEmpty(keyword) && StringUtil.isEmpty(describe) && StringUtil.isEmpty(createTime)){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/operation/viewList")){
                data.put("datas", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Date beginTime = null, endTime = null;
        if (!StringUtil.isEmpty(createTime)) {
            String[] arr = createTime.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = adminOperationLogMapper.list(p, keyword, describe, beginTime, endTime);
        List<Map<String, Object>> datas = iPage.getRecords();

        data.put("datas", datas);
        data.put("total", iPage.getTotal());

        addOperationLog(request, "获取操作日志数据列表");

        return Result.returnSuccess(data);
    }
}
